package com.ihrm.common.utils;



import java.security.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {
    
    /**
     * 日期转换 String->Date
     */
    public static Date parseString2Date(String dateString) throws ParseException{
    
        if(dateString==null){
            return null;
        }
        return parseString2Date(dateString,"yyyy-MM-dd");
    }
    
    /**
     * 日期转换 String->Date
     */
    public static Date parseString2Date(String dateString,String pattern) throws ParseException{
    
        if(dateString==null){
            return null;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        Date date = dateFormat.parse(dateString);
        return date;
    }
    
    /**
     * 日期转换 Date->String
     */
    public static String parseDate2String(Date date) throws ParseException{
    
        if(date==null){
            return null;
        }
        return parseDate2String(date,"yyyy-MM-dd");
    }
    
    /**
     * 日期转换 Date->String
     */
    public static String parseDate2String(Date date,String pattern){
    
        if(date==null){
            return null;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        String format = dateFormat.format(date);
        return format;
    }
    
    public static String getFirstDayOfMonth(int year,int month){
    
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR,year);
        calendar.set(Calendar.MONTH,month-1);
        int lastDay = calendar.getActualMaximum(Calendar.DATE);
        calendar.set(Calendar.DAY_OF_MONTH,lastDay);
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM.dd");
        return dateFormat.format(calendar.getTime());
    }
    
    public static String getLastDayOfMonth1(int year,int month){
    
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR,year);
        calendar.set(Calendar.MONTH,month-1);
        int lastDay = calendar.getActualMaximum(Calendar.DATE);
        calendar.set(Calendar.DAY_OF_MONTH,lastDay);
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM.dd");
        return dateFormat.format(calendar.getTime());
    }
    
    public static String getStrMonth(int month){
    
        String[] strs={"一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"};
        if(month>0&&month<13){
            return strs[month-1];
        }else {
            return "一";
        }
    }
    
    public static String parseTimestampToStr(Timestamp timestamp,String timeFormat){
    
        SimpleDateFormat dateFormat = new SimpleDateFormat(timeFormat);
        return dateFormat.format(timestamp);
    }
    
    public static String timestampToDate(long time,String timeFormat){
    
        if(time<10000000000L){
            time=time*1000;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(timeFormat);
        String s = dateFormat.format(new Date(Long.parseLong(String.valueOf(time))));
        return s;
    }
    
    public static List<String>  getYearMonths() throws Exception{
    
        ArrayList<String> result = new ArrayList<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMM");
        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();
        min.set(min.get(Calendar.YEAR),0,1);
        max.setTime(new Date());
        max.set(max.get(Calendar.YEAR),max.get(Calendar.MONTH),2);
        Calendar curr = max;
        while (curr.after(min)){
            result.add(dateFormat.format(curr.getTime()));
            curr.add(Calendar.MONTH,-1);
        }
        return result;
    }
    
    public static String[] getDaysByYearMonth(String yearMonth) throws ParseException{
    
        Date date = new SimpleDateFormat("yyyyMM").parse(yearMonth);
        Calendar a = Calendar.getInstance();
        a.setTime(date);
        a.set(Calendar.DATE,1);
        a.roll(Calendar.DATE,-1);
        int maxDate = a.get(Calendar.DATE);
        String[] days = new String[maxDate];
        for(int i=0;i<maxDate;i++){
            String day= (i+1)<10 ? ("0"+(i+1)) : ((i+1)+"");
            days[i]=yearMonth+day;
        }
        return days;
    }
    
    public static boolean comparingDate(String target,Date sourceDate) throws Exception{
    
        Date date1 = new SimpleDateFormat("HH:mm:ss").parse(target);
        String t = new SimpleDateFormat("HH:mm:ss").format(sourceDate);
        Date date2 = new SimpleDateFormat("HH:mm:ss").parse(t);
        return date1.after(date2);
    }
    
    public static boolean isWeekend(String time) throws ParseException{
    
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parseString2Date(time,"yyyyMMdd"));
        int i=calendar.get(Calendar.DAY_OF_WEEK)-1;
        return i==0?true:i==5?true:false;
    }
    
    public static void main(String[] args) throws ParseException {
        System.out.println("202101".substring(4));
    }
    
}
